package com.changgou.order.service.impl;

import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    private static final String  CART= "cart_";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    /**
     *添加购物车
     * @param skuId 商品id，用于查询商品具体数据
     * @param num 数量
     * @param username 用户名，根据用户名来确定谁的购物车
     */
    @Override
    public void addCart(String skuId, Integer num, String username) {

        //查询redis有无商品信息
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART+username).get(skuId);

        if (orderItem != null) {
            //有 修改商品价格和数量
            orderItem.setNum(orderItem.getNum() + num);
            if(orderItem.getNum()<=0){
                redisTemplate.boundHashOps(CART+username).delete(skuId);
                return;
            }
            orderItem.setMoney(orderItem.getNum() * orderItem.getPrice());
            orderItem.setPayMoney(orderItem.getNum() * orderItem.getPrice());
        } else {
            //无 把商品相关的数据添加到redis中
            Sku sku = skuFeign.findById(skuId).getData();
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();
            orderItem = this.sku2OrderItem(sku, spu, num);
        }

        redisTemplate.boundHashOps(CART+username).put(skuId,orderItem);
    }

    /**
     * 查看购物车列表
     * @param username
     * @return 返回一个map集合，存储总价钱，总的商品数，商品列表的数据
     */
    @Override
    public Map list(String username) {
        Map map = new HashMap<>();
        //redis中hash结构     Key field value  这里拿到value
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        Integer totalNum = 0 ;
        Integer totalMoney = 0 ;
        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();
            totalMoney += orderItem.getMoney();
        }
        map.put("orderItemList",orderItemList);
        map.put("totalNum",totalNum);
        map.put("totalMoney",totalMoney);

        return map;
    }

    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num * orderItem.getPrice()); //单价*数量
        orderItem.setPayMoney(num * orderItem.getPrice()); //实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num); //重量=单个重量*数量

        //分类ID设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());

        return orderItem;
    }


}
